package com.weijp.testdemo;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.github.chrisbanes.photoview.PhotoView;

import me.relex.photodraweeview.PhotoDraweeView;

public class MainActivity extends AppCompatActivity {

    private PhotoDraweeView photoDraweeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //PhotoView photoView=(PhotoView)findViewById(R.id.photo_view);
        //photoView.setImageResource(R.mipmap.timg);


        photoDraweeView = (PhotoDraweeView)findViewById(R.id.photodraweeview);
        //photoDraweeView.setPhotoUri(Uri.parse(""));
        initData();
    }

    private void initData() {
       String img_url = "https://mmbiz.qpic.cn/mmbiz_jpg/KKlWx3o7UM1iah1psy6lLdLL2gaiaXs1DUWI34f1es0xSDGG7px1I93ccUmYwAR2yoJyIgp2nH1rJyV7Q05Fjdag/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1";//getIntent().getStringExtra("img_url");
        if (!TextUtils.isEmpty(img_url)) {
            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            //本地图片使用"file://"
            controller.setUri(img_url);//设置图片url
            controller.setOldController(photoDraweeView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null || photoDraweeView == null) {
                        return;
                    }
                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            photoDraweeView.setController(controller.build());
        } else {
            Toast.makeText(this, "图片获取失败", Toast.LENGTH_SHORT).show();
        }
    }
}
